package Reflect;

import java.lang.reflect.Constructor;

public class TestDemo01 {


    public static void main(String[] args) {
        getConstructors();
    }


    //获取全部构造器对象
    public  static  void getConstructors(){
        Class c =Student.class;
        Constructor[] constructors = c.getConstructors();
        for (Constructor constructor : constructors) {
            System.out.println(constructor.getName()+"========>"+constructor.getParameterCount());
        }
    }


    //获取单个构造器对象
    public  static  void getConstructor() throws Exception {
        Class c =Student.class;
        //按照参数定位无参数构造器
        Constructor constructor = c.getConstructor();
            System.out.println(constructor.getName()+"========>"+constructor.getParameterCount());

    }

    //可获取私有，无所谓权限
    public  static  void getDeclaredConstructor() throws Exception {
        Class c =Student.class;
        //按照参数定位无参数构造器
        Constructor constructor = c.getDeclaredConstructor();
        System.out.println(constructor.getName()+"========>"+constructor.getParameterCount());
        //暴力反射 针对于private 构造器
        constructor.setAccessible(true);
        Student s = (Student) constructor.newInstance();
    }
}
